<?php
/**
 * Created by Yevgen Vershynin.
 * User: Yevgen Vershynin
 * Description: Controller for Office entity. Controls all operations for incoming requests.
 */

class OfficeController extends BaseController
{
    private $officeModel;

    public function __construct()
    {
        $this->officeModel = new OfficeModel();
    }

    public function getAction($request)
    {
        //Fetch data according to request arguments.
        if(isset($request->urlElements[2]))
        {
            //Fetch Office by number
            $officeId = (int)$request->urlElements[2];
            $data["message"] = "here is the info for office " . $officeId;
            $office = $this->officeModel->getOfficeById($officeId);
            $data["office"] = $office;
        }
        else
        {
            //Fetch all offices.
            $data["message"] = "you want a list of offices";
            $offices = $this->officeModel->getAllOffices();
            $data["offices"] = $offices;
        }
        return $data;
    }

    public function postAction($request)
    {
        $data = $request->parameters;
        //Update Office entry by Office number.
        if(isset($request->urlElements[2]))
        {
            if($this->officeModel->updateOffice($data))
            {
                $data['message'] = "This data was submitted";
            }
            else
            {
                $data['message'] = "Data not saved. DB error.";
            }
        }
        else
        {
            $data['message'] = "Data not saved. Office id is not provided.";
        }

        return $data;
    }

    public function putAction($request) {
        $data = $request->parameters;

        //Create Office entry
        if($this->officeModel->createOffice($data))
        {
            $data['message'] = "This data was submitted";
        }
        else
        {
            $data['message'] = "Data not saved.";
        }

        return $data;
    }

    public function deleteAction($request) {
        $data = $request->parameters;

        //Delete Office entry by Office number
        if(isset($request->urlElements[2])) {
            $officeId = (int)$request->urlElements[2];

            if($this->officeModel->deleteOffice($officeId))
            {
                $data['message'] = "This office was deleted";
            }
            else
            {
                $data['message'] = "This office was NOT deleted. DB error.";
            }
        }
        else
        {
            $data['message'] = "This office was NOT deleted. Office id is not provided";
        }

        return $data;
    }

}